/********************************************************************************
* File Name:	mainform.cpp
* Description:	第14章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "mainform.h"
#include "ui_mainform.h"
#include "topscore.h"
#include "input.h"
#include <QtGui/QApplication>
#include <QMessageBox>
#include <QtSql>
#include "database.h"

Mainform::Mainform(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Mainform)
{
    ui->setupUi(this);
    this->score = 0;
    this->resize(600,440);
    this->gameArea = new GameArea(this);
    this->timer = new QTimer(this);
    this->gameArea->setmovespeed(500);
    connect(this->timer,SIGNAL(timeout()),this,SLOT(timer_upDate()));
}
Mainform::~Mainform()
{
    delete ui;
}
void Mainform::timer_upDate()
{
    this->gameArea->moveOneStep();
    if(this->gameArea->isMoveEnd())
    {
        if(this->gameArea->isGame_Over())
        {
            this->timer->stop();
            QMessageBox::warning(this,tr("warning"),tr("Game Over!"),QMessageBox::Yes);
            ui->btnStart->setEnabled(true);

            int minscore,count;
            if(!database::Open())
            {
                QMessageBox::about(this,"Message","connect error");
                return;
            }
            QSqlQuery query;
            query.exec("select min(Score) from list");
            while(query.next())
            {
                minscore=query.value(0).toInt();
            }
            query.exec("select count(*) from list");
            while(query.next())
            {
                count=query.value(0).toInt();
            }
            database::Close();
            if(num>minscore || count<5)
            {

                Input onrank;
                onrank.score=num;
                onrank.sum=count;
                onrank.show();
                onrank.exec();
            }
            else
                QMessageBox::warning(this,tr("warning"),tr("So sorry"),QMessageBox::Yes);
            ui->lb_score->setText("0");
            this->gameArea->setscore(0);
            //this->gameArea->init_Game();
            //this->gameArea->gameStart();
            //this->timer->start(this->gameArea->getmovespeed());
        }
        else
        {
            this->gameArea->nextItem();
            num = this->gameArea->getscore();
            QString final_score=QString::number(num,10);
            ui->lb_score->setText(final_score);
            this->gameArea->gameStart();
        }
    }
    else
    {
        this->gameArea->do_MoveNext();
    }
}
void Mainform::on_btnStart_clicked()
{
    this->gameArea->init_Game();
    this->gameArea->gameStart();
    this->timer->start(this->gameArea->getmovespeed());
    this->gameArea->setFocus();
    ui->btnStart->setEnabled(false);
}
void Mainform::on_btnList_clicked()
{
    Topscore diag;
    diag.show();
    diag.exec();
}
void Mainform::on_btnSlowDown_clicked()
{
    this->gameArea->setmovespeed(200);
}
void Mainform::on_btnSpeedUp_clicked()
{
    this->gameArea->setmovespeed(500);
}
